{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>feat_1</th>\n",
       "      <th>feat_2</th>\n",
       "      <th>feat_3</th>\n",
       "      <th>feat_4</th>\n",
       "      <th>feat_5</th>\n",
       "      <th>feat_6</th>\n",
       "      <th>feat_7</th>\n",
       "      <th>feat_8</th>\n",
       "      <th>feat_9</th>\n",
       "      <th>...</th>\n",
       "      <th>feat_84_tfidf</th>\n",
       "      <th>feat_85_tfidf</th>\n",
       "      <th>feat_86_tfidf</th>\n",
       "      <th>feat_87_tfidf</th>\n",
       "      <th>feat_88_tfidf</th>\n",
       "      <th>feat_89_tfidf</th>\n",
       "      <th>feat_90_tfidf</th>\n",
       "      <th>feat_91_tfidf</th>\n",
       "      <th>feat_92_tfidf</th>\n",
       "      <th>feat_93_tfidf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.412891</td>\n",
       "      <td>0.058633</td>\n",
       "      <td>0.841374</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.031250</td>\n",
       "      <td>0.044444</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.195122</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.144047</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.06132</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.022222</td>\n",
       "      <td>0.142857</td>\n",
       "      <td>0.012195</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.078015</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.070429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.012195</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.135478</td>\n",
       "      <td>0.033424</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0.015625</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.012195</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.022727</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.547803</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 187 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id    feat_1    feat_2    feat_3    feat_4  feat_5  feat_6    feat_7  \\\n",
       "0   1  0.000000  0.000000  0.000000  0.000000     0.0     0.0  0.000000   \n",
       "1   2  0.031250  0.044444  0.166667  0.195122     0.0     0.0  0.000000   \n",
       "2   3  0.000000  0.022222  0.142857  0.012195     0.0     0.0  0.000000   \n",
       "3   4  0.000000  0.000000  0.000000  0.012195     0.0     0.0  0.000000   \n",
       "4   5  0.015625  0.000000  0.000000  0.012195     0.0     0.0  0.022727   \n",
       "\n",
       "   feat_8  feat_9  ...  feat_84_tfidf  feat_85_tfidf  feat_86_tfidf  \\\n",
       "0    0.00     0.0  ...            0.0       0.000000       0.412891   \n",
       "1    0.00     0.0  ...            0.0       0.000000       0.000000   \n",
       "2    0.00     0.0  ...            0.0       0.000000       0.000000   \n",
       "3    0.00     0.0  ...            0.0       0.135478       0.033424   \n",
       "4    0.02     0.0  ...            0.0       0.000000       0.000000   \n",
       "\n",
       "   feat_87_tfidf  feat_88_tfidf  feat_89_tfidf  feat_90_tfidf  feat_91_tfidf  \\\n",
       "0       0.058633       0.841374       0.000000            0.0       0.000000   \n",
       "1       0.000000       0.000000       0.144047            0.0       0.000000   \n",
       "2       0.000000       0.078015       0.000000            0.0       0.000000   \n",
       "3       0.000000       0.000000       0.000000            0.0       0.000000   \n",
       "4       0.000000       0.000000       0.000000            0.0       0.547803   \n",
       "\n",
       "   feat_92_tfidf  feat_93_tfidf  \n",
       "0        0.00000       0.000000  \n",
       "1        0.06132       0.000000  \n",
       "2        0.00000       0.070429  \n",
       "3        0.00000       0.000000  \n",
       "4        0.00000       0.000000  \n",
       "\n",
       "[5 rows x 187 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import _pickle as cPickle\n",
    "test1 = pd.read_csv('data/Otto_FE_test_org.csv')\n",
    "# test2 = pd.read_csv('data/Otto_FE_test_tfidf.csv')\n",
    "# test1.head()\n",
    "#增加了维数\n",
    "# test = pd.concat([test1,test2],axis = 1,ignore_index=False)\n",
    "test = test1\n",
    "test.head()\n",
    "test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "test_id = test['id']\n",
    "x_test = test.drop(['id'],axis = 1)\n",
    "feat_names = x_test.columns\n",
    "from scipy.sparse import csr_matrix\n",
    "x_test = csr_matrix(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'scipy.sparse.csr.csr_matrix'>\n"
     ]
    }
   ],
   "source": [
    "print(type(x_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果要联合特征训练的模型来预测，那么训练的时候就需要联合特征训练\n",
    "lr_best = cPickle.load(open('data/Otto_L1_org.pkl','rb'))\n",
    "# 预测概率 predict_proba\n",
    "y_test_pred = lr_best.predict_proba(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(144368, 9)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test_pred.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>Class_1</th>\n",
       "      <th>Class_2</th>\n",
       "      <th>Class_3</th>\n",
       "      <th>Class_4</th>\n",
       "      <th>Class_5</th>\n",
       "      <th>Class_6</th>\n",
       "      <th>Class_7</th>\n",
       "      <th>Class_8</th>\n",
       "      <th>Class_9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.207664e-04</td>\n",
       "      <td>0.284878</td>\n",
       "      <td>0.415949</td>\n",
       "      <td>2.870854e-01</td>\n",
       "      <td>3.814008e-11</td>\n",
       "      <td>0.000469</td>\n",
       "      <td>0.011279</td>\n",
       "      <td>1.579062e-05</td>\n",
       "      <td>0.000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>8.101074e-04</td>\n",
       "      <td>0.293678</td>\n",
       "      <td>0.002040</td>\n",
       "      <td>1.152401e-06</td>\n",
       "      <td>3.850412e-05</td>\n",
       "      <td>0.274908</td>\n",
       "      <td>0.005829</td>\n",
       "      <td>4.194751e-01</td>\n",
       "      <td>0.003221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>8.598225e-04</td>\n",
       "      <td>0.000416</td>\n",
       "      <td>0.000151</td>\n",
       "      <td>9.446985e-08</td>\n",
       "      <td>4.778336e-08</td>\n",
       "      <td>0.996837</td>\n",
       "      <td>0.000705</td>\n",
       "      <td>9.487929e-04</td>\n",
       "      <td>0.000083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4.356507e-07</td>\n",
       "      <td>0.683318</td>\n",
       "      <td>0.273204</td>\n",
       "      <td>4.341393e-02</td>\n",
       "      <td>5.870138e-09</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>3.050310e-07</td>\n",
       "      <td>0.000061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>5.120656e-01</td>\n",
       "      <td>0.002504</td>\n",
       "      <td>0.000700</td>\n",
       "      <td>8.610602e-06</td>\n",
       "      <td>9.500232e-13</td>\n",
       "      <td>0.011270</td>\n",
       "      <td>0.011492</td>\n",
       "      <td>2.178821e-01</td>\n",
       "      <td>0.244077</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id       Class_1   Class_2   Class_3       Class_4       Class_5   Class_6  \\\n",
       "0   1  3.207664e-04  0.284878  0.415949  2.870854e-01  3.814008e-11  0.000469   \n",
       "1   2  8.101074e-04  0.293678  0.002040  1.152401e-06  3.850412e-05  0.274908   \n",
       "2   3  8.598225e-04  0.000416  0.000151  9.446985e-08  4.778336e-08  0.996837   \n",
       "3   4  4.356507e-07  0.683318  0.273204  4.341393e-02  5.870138e-09  0.000001   \n",
       "4   5  5.120656e-01  0.002504  0.000700  8.610602e-06  9.500232e-13  0.011270   \n",
       "\n",
       "    Class_7       Class_8   Class_9  \n",
       "0  0.011279  1.579062e-05  0.000002  \n",
       "1  0.005829  4.194751e-01  0.003221  \n",
       "2  0.000705  9.487929e-04  0.000083  \n",
       "3  0.000001  3.050310e-07  0.000061  \n",
       "4  0.011492  2.178821e-01  0.244077  "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成提交结果\n",
    "out_df = pd.DataFrame(y_test_pred)\n",
    "out_df.head()\n",
    "columns = np.empty(9,dtype = object)\n",
    "for i in range(9):\n",
    "    columns[i] = 'Class_'+str(i+1)\n",
    "out_df.columns = columns\n",
    "out_df = pd.concat([test_id,out_df],axis = 1)\n",
    "out_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_df.to_csv('data/LR_Org.csv',index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Class_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>Class_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0\n",
       "0  Class_3\n",
       "1  Class_8\n",
       "2  Class_6\n",
       "3  Class_2\n",
       "4  Class_1\n",
       "5  Class_2\n",
       "6  Class_8\n",
       "7  Class_2\n",
       "8  Class_2\n",
       "9  Class_8"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test_pred_c= lr_best.predict(x_test)\n",
    "out_df_c = pd.DataFrame(y_test_pred_c)\n",
    "out_df_c.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
